package com.oa.hyperion.repository.assets;

import com.oa.hyperion.entity.assets.AssetsGeneralLedger;
import com.oa.hyperion.utils.DBUtils;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.jdbc.SQL;

import java.util.List;
/**
 * 资产总账
 * author yuanchongwei
 * date 2022-1-18
 */
public interface AssetsGeneralLedgerMapper {
    /**
     * 获取表中所有资产信息
     * @return 资产列表
     */
    @Select("select * from assets_general_ledger")
    List<AssetsGeneralLedger> getAllAssets();

    /**
     * 单个条件查询
     * 根据资产类型查询
     * 根据资产状态查询
     * 根据所属部门查询
     * @param assets 资产
     * @return 资产列表
     */
    @SelectProvider(type = AssetsGeneralLedgerMapper.SqlProvider.class,method = "select")
    List<AssetsGeneralLedger> selectAssets(AssetsGeneralLedger assets);

    /**
     * 根据资产id查询
     * @param id 资产
     * @return 资产列表
     */
    @Select("select * from assets_general_ledger where id=#{id}")
    AssetsGeneralLedger getAssetsById(int id);

    /**
     * 根据资产name查询
     * @param barCode 条码
     * @return 资产列表
     */
    @Select("select * from assets_general_ledger where bar_code=#{barCode}")
    AssetsGeneralLedger getAssetsByBarCode(String barCode);

    /**
     * 多条件查询
     * 根据资产状态和类型查询
     * @param type 类型
     * @param status 状态
     * @return 资产列表
     */
    @Select("select * from assets_general_ledger where type=#{type} and status=#{status}")
    List<AssetsGeneralLedger> getAssetsByTypeAndStatus(@Param("type") int type,@Param("status") int status);

    /**
     * 新增资产时，将资产信息写入总账表
     * @param assets 资产信息
     */
    @Insert("insert into " +
            "assets_general_ledger(name,model,price,bar_code,type,entry_date,first_install_date,stock,repair_times,status,belongs_to,user) " +
            "values(#{name},#{model},#{price},#{barCode},#{type},#{entryDate},#{firstInstallDate},#{stock},#{repairTimes},#{status},#{belongsTo},#{user})")
    @Options(useGeneratedKeys = true,keyColumn = "id",keyProperty = "id")
    boolean insert(AssetsGeneralLedger assets);


    /**
     * 修改资产信息
     * 场景：维修，借出、采购、调拨、报废
     * @param assets 资产信息
     */
    @UpdateProvider(type = AssetsGeneralLedgerMapper.SqlProvider.class,method = "update")
    boolean update(AssetsGeneralLedger assets);

    public static class SqlProvider{
        /**
         * 根据资产类型查询
         * 根据资产状态查询
         * 根据所属部门查询
         * @param assets 资产
         * @return 返回拼接sql
         */
        public static String select(AssetsGeneralLedger assets){
            return new SQL(){
                {
                    SELECT("*");
                    FROM("assets_general_ledger");
                    if(assets.getType() != 0){
                        WHERE("type=#{type}");
                    }
                    if(assets.getStatus() != 0){
                        WHERE("status=#{status}");
                    }
                    if(assets.getBelongsTo() != 0){
                        WHERE("belongs_to=#{belongsTo}");
                    }
                }
            }.toString();
        }
        public static String update(AssetsGeneralLedger assets){
            SQL sql = new SQL();
            sql.UPDATE("assets_general_ledger");
            if(assets.getStock() > 0){
                //修改库存
                sql.SET("stock=#{stock}");
            }
            if (assets.getRepairTimes() > 0) {
                //修改维修次数
                sql.SET("repair_times=#{repairTimes}");
            }
            if (assets.getStatus() > 0) {
                //修改状态
                sql.SET("status=#{status}");
            }
            if (assets.getBelongsTo() > 0) {
                //修改所属部门
                sql.SET("belongs_to=#{belongsTo}");
            }
            sql.WHERE("id=#{id}");

            return sql.toString();
        }
    }
}
